//随笔时间格式化脚本
const { writeFile } = require('fs')
const data = require('../essays/index.json')

const padZero = num => (num < 10 ? `0${num}` : num)
const formatDate = date => {
  const [year, month, day] = date.split('-')
  return `${year}-${padZero(month)}-${padZero(day)}`
}

async function main() {
  data.essays.forEach(essay => {
    essay.date = formatDate(essay.date)
  })
  await writeFile('essays/index.json', JSON.stringify(data, null, 2), err => {
    if (err) {
      console.log('An error has occurred ', err)
      return
    }
    console.log('written successfully')
  })
}

main()
